package com.asset.dispoal.business.appoint.service.command;

import com.asset.dispoal.business.appoint.domain.db.mapper.AppointMapper;
import com.asset.dispoal.business.appoint.dto.AppointPageRequest;
import com.asset.dispoal.business.appoint.dto.AppointPageResponse;
import com.asset.dispoal.commom.page.PagingQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

@Component
@Slf4j
public class QueryPageAppointCommand implements BaseCommand<AppointPageRequest, Result<PageVO<AppointPageResponse>>> {

    @Autowired
    AppointMapper appointMapper;

    @Override
    public Result<PageVO<AppointPageResponse>> execute(AppointPageRequest request) {
        request.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int count = appointMapper.queryCount(request);
        Page<AppointPageRequest> page = PagingQuery.getPage(count, request);
        PageVO<AppointPageResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<AppointPageResponse> bsCases = appointMapper.queryPage(page);
            pageVO.setResult(bsCases);
        }
        return Result.ok(pageVO);
    }
}
